package com.xiaoKe.zhonghzu.ui.gather;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;

import androidx.viewbinding.ViewBinding;

import com.hjq.permissions.OnPermissionCallback;
import com.hjq.permissions.Permission;
import com.hjq.permissions.XXPermissions;
import com.xiaoKe.zhonghzu.R;
import com.xiaoKe.zhonghzu.global.base.BaseApplication;
import com.xiaoKe.zhonghzu.global.base.BaseFragment;
import com.xiaoKe.zhonghzu.global.base.VeinRouter;
import com.xiaoKe.zhonghzu.global.bean.ClFzData;
import com.xiaoKe.zhonghzu.global.tool.JsonUtils;
import com.xiaoKe.zhonghzu.global.tool.ToastUtils;
import com.xiaoKe.zhonghzu.ui.onClick.NoDoubleClickListener;
import com.yzq.zxinglibrary.android.CaptureActivity;
import com.yzq.zxinglibrary.bean.ZxingConfig;
import com.yzq.zxinglibrary.common.Constant;

import java.util.List;
import java.util.Map;

import static android.app.Activity.RESULT_OK;

/**
 * @ProjectName : xiaoke-pad-zhongzhu
 * @Author : Shijingna
 * @Time : 2024/12/19 10:06
 * @Description : 文件描述
 */
public class CodeFragment<T extends ViewBinding> extends BaseFragment<T> {
    public static final int REQUEST_CODE_SCAN = 100;

    @Override
    protected void initView() {

    }

    //扫一扫
    public void sweep() {
        XXPermissions.with(getContext())
                // 申请单个权限
                .permission(Permission.CAMERA)
                .request(new OnPermissionCallback() {
                    @Override
                    public void onGranted(List<String> permissions, boolean all) {
                        if (all) {
                            //说明已经获取到摄像头权限了
                            Intent intent = new Intent(getContext(), CaptureActivity.class);
                            ZxingConfig config = new ZxingConfig();
                            config.setShowbottomLayout(true);//底部布局（包括闪光灯和相册）
                            config.setPlayBeep(true);//是否播放提示音
                            config.setShake(true);//是否震动
                            config.setShowAlbum(true);//是否显示相册
                            startActivityForResult(intent, REQUEST_CODE_SCAN);
                        } else {
                            ToastUtils.showLong("获取部分权限成功，但部分权限未正常授予");
                        }
                    }

                    @Override
                    public void onDenied(List<String> permissions, boolean never) {
                        if (never) {
                            ToastUtils.showLong("被永久拒绝授权，请手动授予相机权限权限");
                            // 如果是被永久拒绝就跳转到应用权限系统设置页面
                            XXPermissions.startPermissionActivity(BaseApplication.instance, permissions);
                        } else {
                            ToastUtils.showLong("获取相机权限失败");
                        }
                    }
                });


    }

    @Override
    protected void initData() {

    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        // 扫描二维码/条码回传
        if (requestCode == REQUEST_CODE_SCAN && resultCode == RESULT_OK) {
            if (data != null) {
                String content = data.getStringExtra(Constant.CODED_CONTENT);
                try {
                    ClFzData map = JsonUtils.fromJson(content, ClFzData.class);
//                    ToastUtils.showToast("扫描结果为：" + content);
                    Bundle bundle = new Bundle();
                    map.setInputType(1);
                    bundle.putSerializable("data",map);
                    toFragmentPage(VeinRouter.REPRODUCTIVEADDFRAGMENT.setBundle(bundle));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        } else if (requestCode == XXPermissions.REQUEST_CODE) {
            if (XXPermissions.isGranted(getContext(), Permission.CAMERA)) {
                sweep();
            } else {
                ToastUtils.showLong("用户没有在权限设置页授予权限");
            }
        }
    }
}
